<?php

namespace App\Services;

use App\UserApp;
use Illuminate\Support\Facades\Redis;

/**
 * Class AppService
 *
 * @package \App\Services
 */
class AppService extends BaseService
{
    /**
     * 口子调用
     *
     * @param $appId
     *
     * @return array
     */
    public function getKouZiResult($appId)
    {
        $url = "https://api.yingbigege.com/api/v1/search";
        $params = [] ; // 'code' => "3nb8", "" => "飞花宝"

        // 获取App的参数
        $queryParam = UserApp::where("id", $appId)->value("query_param");

        if (!empty($queryParam)) {
            // 处理参数
            $arr = explode("&", $queryParam);

            foreach ($arr as $v) {
                $tmp = explode("=", $v);
                $params[$tmp[0]] = $tmp[1];
            }

            $res = Redis::get($queryParam);
            if (empty($res)) {
                $result = $this->curlPost($url, $params);
                \Log::info($result);
                Redis::set($queryParam, json_encode($result));
            } else {
                $result = json_decode($res, true);
            }

            return !empty($result["data"]) ? $result["data"]["data"] : [];
        }

    }

}
